<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\Attribute;
use App\Model\AttributeValue;
use App\Model\Category;
use DB;

class AttributeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $lists = Attribute::with('category')->where('status', '!=', -1)->paginate(15);

        return view('admin.attribute.index', compact('lists'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $categorys = Category::with('allChildrenCategorys')->where('parent_id', 0)->get();
// dd($categorys);
        return view('admin.attribute.create', compact('categorys'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        DB::beginTransaction();
        try {
            $datas = $request->except(['_token', 'value']);
            $createA = Attribute::create($datas);
            $value = $request->value;
            // $createAV = AttributeValue::create($);
            $values = explode('|', $value);
            foreach ($values as $key => $val) {
                AttributeValue::create(['value'=>$val, 'attr_id'=>$createA->id]);
            }
            DB::commit();
            return parent::success('success', route('admin.attribute.index'));
        } catch (Exception $e) {
            DB::rollback();
            return parent::error('error');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $attr      = Attribute::find($id);
        $categorys = Category::with('allChildrenCategorys')->where('parent_id', 0)->get();
        $attrVals  = AttributeValue::where('attr_id', $id)->pluck('value')->toArray();
        $valsStr   = implode('|', $attrVals);

        return view('admin.attribute.edit', compact('attr', 'valsStr', 'categorys'));        
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request)
    {
        DB::beginTransaction();
        try {
            $datas   = $request->except('_token', 'value', 'attr_id');
            $valuesStr  = $request->value;
            $values = explode('|', $valuesStr);
            $id      = $request->attr_id;
            $updateA = Attribute::where('id', $id)->update($datas);
            $ids     = AttributeValue::where('attr_id', $id)->pluck('id');
            $del     = AttributeValue::destroy($ids->toArray());

            foreach ($values as $key => $value) {
                AttributeValue::create(['value'=>$value, 'attr_id'=>$id]);
            }
            DB::commit();
            return parent::success('success', route('admin.attribute.index'));
        } catch (Exception $e) {
            DB::rollback();
            return parent::error('error');
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $update = Attribute::where('id', $id)->update(['status'=>-1]);
        if ($update) {
            return parent::success('success');
        } else {
            return parent::error('error');
        }  
    }

    public function destroyAll(Request $request)
    {
        $attr_ids = $request->attr_ids;
            // return parent::success($attr_ids);

        $update = Attribute::whereIn('id', $attr_ids)->update(['status'=>-1]);
        if ($update) {
            return parent::success('success');
        } else {
            return parent::error('error');
        } 
    }

    public function publish($id)
    {
        $update = Attribute::where('id', $id)->update(['status'=>1]);
        if ($update) {
            return parent::success('success');
        } else {
            return parent::error('error');
        }        
    }

    public function unpublish($id)
    {
        $update = Attribute::where('id', $id)->update(['status'=>0]);
        if ($update) {
            return parent::success('success');
        } else {
            return parent::error('error');
        }        
    }    

}
